from bs4 import BeautifulSoup
import requests # 用来获取网页
import chardet

url = 'https://www.oschina.net/'

ua = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36'}

rq = requests.get(url, headers=ua)

rq.encoding = chardet.detect(rq.content)['encoding']

# 初始化html
html = rq.content.decode('utf-8')
# 生成bs对象
soup = BeautifulSoup(html, "lxml")
# print("输出格式化的BeautifulSoup对象：", soup.prettify())


# 第一个任务：获取head标签
print("获取head标签：", soup.head)

# 获取title
# print("获取title:", soup.title.string)

# 如何获取属性content
# 先拿到整个标签
# dom1 = soup.find(attrs={'name': 'keywords'})
# 然后获取content内容
# print(dom1.attrs['content'])

# 课堂作业：获取到name属性是description的content属性内容;
# dom1 = soup.find(attrs={'name': 'description'})
# 然后获取content内容
# print(dom1.attrs['content'])

# 获取body标签中的第一个a标签
print("获取第一个a标签: ", soup.body.a)
# 获取所有名称为a标签的个数
print("所有名称为a标签的个数: ", len(soup.find_all('a')))

# 获取所有a标签的class属性
for item in soup.find_all('a'):
    print('a标签的name：', item.attrs['class'])
